package com.example.sunlight.wzoneunicom.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunlight.wzoneunicom.Origin;
import com.example.sunlight.wzoneunicom.R;
import com.example.sunlight.wzoneunicom.Views;
import com.example.sunlight.wzoneunicom.WifiStrength;
import com.example.sunlight.wzoneunicom.Wifis;
import com.example.sunlight.wzoneunicom.data.Datas;
import com.example.wzsdk.login.Landing;
import com.example.wzsdk.login.LoginState;
import com.example.wzsdk.scan.Search;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener, Search.SearchFish, LoginState {
    TextView single, arborescence, stellate, honeycomb, wifi, text;
    ImageView img_single, img_arborescence, img_stellate, img_honeycomb, img_wifi;
    LinearLayout parent;
    Origin origin;
    Views views;
    Wifis wifis;
    Button msg;
    Origin origin2 = new Origin();
    WifiStrength wifiStrength;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            parent.removeAllViews();
            switch (msg.what) {
                case 0x123:
                    Views.kk = 0;
                    Views.end = origin2.ZigbeeStellate;
                    parent.addView(views);
                    break;
                case 0x456:
                    Views.kk = 1;
                    Views.end = origin2.ZigbeeTree;
                    parent.addView(views);
                    break;
                case 0x789:
                    Views.kk = 2;
                    Views.end = origin2.ZigbeeSingle;
                    parent.addView(views);
                    break;
                case 0x753:
                    Views.kk = 3;
                    Views.end = origin2.ZigbeeNet;
                    parent.addView(views);
                    break;
                case 0x951:
                    parent.addView(wifis);
                    break;
                case 0x8436:
                    search.EngSearch(MainActivity.this);
                    break;
                case 0x9863:
                    text.setText(host);
                    break;
            }
        }
    };
    Search search;
    Landing landing;
    ImageView title;

    int[] save;
    String[] name;
    int[] index = {2, 5, 8, 10, 12};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FindView();
        save = new int[new Origin().dbm.length];
        name = new String[5];
        wifiStrength = new WifiStrength();
        origin = new Origin();
        views = new Views(this, Origin.dbm, save, this, origin);
        wifis = new Wifis(this, wifiStrength, this);
        parent.addView(views);
        ViewUtils.inject(this);
        parent.setOnTouchListener(this);
        initWzSdk();
        search.EngSearch(MainActivity.this);
    }

    public void initWzSdk() {
        search = new Search("lwcaxasfsq");
        landing = new Landing("lwcaxasfsq");
        landing.setLoginState(this);
        search.setSearchFish(this);
    }

    public void FindView() {
        single = (TextView) findViewById(R.id.single);
        arborescence = (TextView) findViewById(R.id.arborescence);
        stellate = (TextView) findViewById(R.id.stellate);
        honeycomb = (TextView) findViewById(R.id.honeycomb);
        wifi = (TextView) findViewById(R.id.wifi);
        text = (TextView) findViewById(R.id.text);

        img_single = (ImageView) findViewById(R.id.img_single);
        img_arborescence = (ImageView) findViewById(R.id.img_arborescence);
        img_stellate = (ImageView) findViewById(R.id.img_stellate);
        img_honeycomb = (ImageView) findViewById(R.id.img_honeycomb);
        img_wifi = (ImageView) findViewById(R.id.img_wifi);
        parent = (LinearLayout) findViewById(R.id.parent);
        msg = (Button) findViewById(R.id.msg);
        title = (ImageView) findViewById(R.id.title);
    }

    public void Reset() {
        img_single.setBackgroundResource(R.drawable.ju_tong);
        img_arborescence.setBackgroundResource(R.drawable.ju_tong);
        img_honeycomb.setBackgroundResource(R.drawable.ju_tong);
        img_wifi.setBackgroundResource(R.drawable.ju_wifi);
        img_stellate.setBackgroundResource(R.drawable.ju_xig);
    }

    int num;
    boolean state = false;

    @OnClick({R.id.title, R.id.single, R.id.arborescence, R.id.stellate, R.id.honeycomb, R.id.wifi, R.id.msg})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title:
                if (state == false) {
                    text.setVisibility(View.VISIBLE);
                    state = true;
                } else {
                    text.setVisibility(View.GONE);
                    state = false;
                }

                break;
            case R.id.stellate:
                num = 0;
                sendHandler(0);
                break;
            case R.id.arborescence:
                num = 1;
                sendHandler(1);
                break;
            case R.id.single:
                num = 2;
                sendHandler(2);
                break;

            case R.id.honeycomb:
                num = 3;
                sendHandler(3);

                break;
            case R.id.wifi:
                num = 4;
                sendHandler(4);
                break;
            case R.id.msg:
                moveTomsg(num);
                break;

        }

    }

    public void moveTomsg(int num) {
        switch (num) {
            case 0:
                Intent stell = new Intent(this, StellateActivity.class);
                startActivity(stell);

                break;
            case 1:
                Intent tree = new Intent(this, TreeActivity.class);
                startActivity(tree);

                break;
            case 2:
                Intent brie = new Intent(this, BriefActivity.class);
                startActivity(brie);

                break;
            case 3:
                Intent honey = new Intent(this, HoneyCombActivity.class);
                startActivity(honey);
                break;
            case 4:
                Intent wifi = new Intent(this, WifiActivity.class);
                startActivity(wifi);
                break;
        }
    }

    public void sendHandler(int num) {
        Reset();
        switch (num) {
            case 0:
                img_stellate.setBackgroundResource(R.drawable.ju_xig_on);
                handler.sendEmptyMessage(0x123);
                break;
            case 1:
                img_arborescence.setBackgroundResource(R.drawable.ju_tong_on);
                handler.sendEmptyMessage(0x456);
                break;
            case 2:
                img_single.setBackgroundResource(R.drawable.ju_tong_on);
                handler.sendEmptyMessage(0x789);
                break;
            case 3:
                img_honeycomb.setBackgroundResource(R.drawable.ju_tong_on);
                handler.sendEmptyMessage(0x753);
                break;
            case 4:
                img_wifi.setBackgroundResource(R.drawable.ju_wifi_on);
                handler.sendEmptyMessage(0x951);
                break;
        }
    }

    float starX, endX;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (view.getId() == R.id.parent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    starX = motionEvent.getX();
                    break;

                case MotionEvent.ACTION_UP:
                    endX = motionEvent.getX();
                    if (endX - starX < 0) {
                        if (Math.abs(endX - starX) > 50) {
                            Log.e("move", "向右");
                            if (num < 4) {
                                sendHandler(++num);
                            } else {
                                num = 4;
                            }
                        }
                    } else {
                        if (Math.abs(endX - starX) > 50) {
                            Log.e("move", "向左");
                            if (num > 0) {
                                sendHandler(--num);

                            } else {
                                num = 0;
                            }
                        }
                    }

                    break;
            }
        }

        return true;
    }

    @Override
    public void SearchFishWifi(List<String[]> list) {
    }

    @Override
    public void SearchFishWna(List<String[]> list) {
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i)[1].equals(Datas.HOST)) {
                    landing.Login(list.get(i)[0], 5800);
                }
            }
        } else {
            search.EngSearch(MainActivity.this);
        }
    }

    int k;
    String host;

    @Override
    public void LoginStatus(boolean b, String s) {
        if (b == true) {
            host = s;
            handler.sendEmptyMessage(0x9863);
            if (s.equals(Datas.HOST)) {
                for (int i = 0; i < landing.getDateList().size(); i++) {
                    if (landing.getDateList().get(i)[0].equals(Datas.UUID)) {
                        name[k] = landing.getDateList().get(i)[5];
                        if (k >= 4) {
                            k = 0;
                        } else {
                            k++;
                        }
                    }
                }
                landing.atnVariable("30020");
            }
        } else {
            k = 0;
            for (int i = 0; i < name.length; i++) {
                name[i] = "";
            }
            for (int i = 0; i < save.length; i++) {
                save[i] = 0;
            }
            search.EngSearch(MainActivity.this);
        }

    }


    @Override
    public void AttentionStarting(String uuid, byte number, String index, String id, String host) {
        for (int i = 0; i < name.length; i++) {
            if (name[i].equals(id)) {
                save[this.index[i]] = number;
            }
        }

    }

    @Override
    public void AttentionProcess(String uuid, byte number, String str, String index, String id, String host) {
        for (int i = 0; i < name.length; i++) {
            if (name[i].equals(id)) {
                save[this.index[i]] = number;
            }
        }
    }

    @Override
    public void SetVarState(String s, byte b, String s1) {

    }

}
